﻿// ==UserScript==
// @name        创世书架
// @namespace   https://userscripts.org/scripts/source/382467.meta.js
// @description 创世书架-还我清静
// @include     http://account.book.qq.com/userfavorite/index.html*
// @version     1.0.0
// @grant       none
// @require     http://code.jquery.com/jquery-1.11.0.js
// ==/UserScript==
;(function(){
	'use strict';	//启用严格模式
	
	var bookmarks = [
		"第1卷 第一章",
		"第1卷 第1章",
		"第1卷 001",
		"楔子",
		"第1卷 序",
		"第1卷 第001章",
		"第1卷 0001章",
		"第1卷 前言",
		"第1卷 第0001章"
	];
	var books_list = new Array();
	
	var time_id = setInterval(myTimeFun,1000);
	function myTimeFun(){
		var content = $("#my_bookshelf").html();
		if(content === null || content === "" || content === undefined){
			
		}else{
			Log("1.书架数据加载完成,移除定时器");
			clearInterval(time_id);
			
			var pageSize = $("#pager a").size();
			$("#pager").hide();
			Log("2.获取书架分页，书架共" + pageSize + "页");
			
			Log("3.循环获取各个页面的书架");
			for (var i = 2; i <= pageSize; i++) {
				GetBookPage(i);
			}

            Log("4.按日期降序排列");
            $("#bookListByBookshelf tr").each(function(index,dom){
                var obj = new Object();
                obj["tr"] = $(this).html();
                obj["date"] = $(this).children("td").eq(4).html();
                books_list.push(obj);
            });
            books_list.sort(function(x,y){
                return -CompareDate(x["date"],y["date"]);
            });
            var books_shelf_html = "";
            $(books_list).each(function(index,dom){
                books_shelf_html +=  "<tr>" + dom.tr + "</tr>";
            });
            $("#bookListByBookshelf").html(books_shelf_html);
            $("#bookListByBookshelf tr:odd").addClass("odd");
			
			Log("5.去除第一章书签");
			$("#bookListByBookshelf a.red2").each(function(index,dom){
				var title = $(dom).attr("title");
				$(bookmarks).each(function(i,n) {
					if(title.indexOf(n) > -1){
						$(dom).remove();
						return false;
					}
				})
			});
			
			Log("5.完成");
		}
	};
	
	function GetBookPage(pageIndex){
		$.ajax({
			url:"/userfavorite/getUserBookShelf.html",
			type:"POST",
			async:false,
			dataType:"json",
			data:
			{
				pageIndex:pageIndex,
				Favoriteid:"",
				type:"",
				randomId:Math.random()
			},
			success:function(data){
				if(data != null && data != ""){
					var html = $(data.ListHTMl).find("#bookListByBookshelf").html();
					$("#my_bookshelf #bookListByBookshelf").append(html);
					Log("3." + (pageIndex - 1) + "第" + pageIndex + "页数据加载完毕");
				}
			}
		});
	};
	
	//调试用
	function Log(msg){
		if(console != undefined){
			console.log(msg);
		}
	}
	
	//比较日期
	function CompareDate(sDate1, sDate2){
		if (sDate1.length > 10) {
            sDate1 = sDate1.substring(0, 10);
        }
        if (sDate2.length > 10) {
            sDate2 = sDate2.substring(0, 10);
        }
        var aDate, oDate1, oDate2, iDays;
        aDate = sDate1.split("-");
        oDate1 = new Date(aDate[0], aDate[1] - 1, aDate[2]); //调用Date的构造函数 
        aDate = sDate2.split("-");
        oDate2 = new Date(aDate[0], aDate[1] - 1, aDate[2]);
        iDays = parseInt((oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数
        return iDays;
	}
})();


